package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.spc_ppk_report.SpcPpkReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.reportUtils.LKingSortUtil;
import com.navi.mould1.service.ISpcPpkReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;
import static com.navi.mould1.reportUtils.ComputeUtil.formatDouble2;

@Slf4j
@Service
public class SpcPpkReportServiceImpl implements ISpcPpkReportService {


    public List<SpcPpkReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        sqlVo.appendSql("SELECT");
        sqlVo.appendSql("b_1.data_desc as toolg_desc, b_2.tool_dsc as tool_desc, A.*, B.*");
        sqlVo.appendSql("FROM icim_jn.spc_ret_sum_rpt_dura A");
        sqlVo.appendSql("INNER JOIN icim_jn.spc_ret_sum_rpt B ON A.sum_seq_id = B.sum_seq_id_fk");
        sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc from icim_jn.bis_data where data_cate = 'TOLG') b_1 ON A.area_id_fk = b_1.data_ext");
        sqlVo.appendSql("LEFT JOIN (select tool_id,tool_dsc from icim_jn.bis_tool ) b_2 ON B.tool_id_fk = b_2.tool_id");
        if (StringUtils.isNotEmpty(conditions.getTool_type())) sqlVo.appendSql("INNER JOIN (select tool_id from bis_tool where unit_typ='" + conditions.getTool_type() + "') E ON E.tool_id = B.tool_id_fk");
        sqlVo.appendSql("WHERE 1=1");
        if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo.appendSql("AND (B.TOOL_ID_FK IN(select tool_id from icim_jn.bis_tool where fab_id_fk in" + jointInSql(conditions.getFab_id()) + ") OR B.TOOL_ID_FK = 'T')");
        if (StringUtils.isNotEmpty(conditions.getToolg_id())) sqlVo.appendSql("AND A.AREA_ID_FK in" + jointInSql(conditions.getToolg_id()));
        if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo.appendSql("AND B.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
        if (StringUtils.isNotEmpty(conditions.getCollect_type())) sqlVo.appendSql("AND A.COL_TYP_FK in" + jointInSql(conditions.getCollect_type()));
        if (StringUtils.isNotEmpty(conditions.getReport_type())) sqlVo.appendSql("AND A.RPT_TYP_FK in" + jointInSql(conditions.getReport_type()));
        if (StringUtils.isNotEmpty(conditions.getData_group())) sqlVo.appendSql("AND B.DATA_GROUP_FK in" + jointInSql(conditions.getData_group()));
        if (StringUtils.isNotEmpty(conditions.getControl_chart_grp())) {
            String grpNo_list_str = Arrays.stream(conditions.getControl_chart_grp().split(",")).map(data -> "'"+data.split("@")[0]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            String grpName_list_str = Arrays.stream(conditions.getControl_chart_grp().split(",")).map(data -> "'"+data.split("@")[2]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            sqlVo.appendSql("AND B.grp_no_fk in" + grpNo_list_str);
            sqlVo.appendSql("AND B.grp_name_fk in" + grpName_list_str);
        }
        if (StringUtils.isNotEmpty(conditions.getControl_chart())) {
            String chartNo_list_str = Arrays.stream(conditions.getControl_chart().split(",")).map(data -> "'"+data.split("@")[0]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            String chartTyp_list_str = Arrays.stream(conditions.getControl_chart().split(",")).map(data -> "'"+data.split("@")[1]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            String chartName_list_str = Arrays.stream(conditions.getControl_chart().split(",")).map(data -> "'"+data.split("@")[2]+"'").distinct().collect(Collectors.joining(",", "(", ")"));
            sqlVo.appendSql("AND B.chart_no_fk in" + chartNo_list_str);
            sqlVo.appendSql("AND B.chart_typ_fk in" + chartTyp_list_str);
            sqlVo.appendSql("AND B.chart_name_fk in" + chartName_list_str);
        }
        if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND A.RPT_E_DATE >= ?", conditions.getStart_time());
        if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND A.RPT_B_DATE <= ?", conditions.getEnd_time());
        sqlVo.appendSql("ORDER BY B.CALC_E_PROC_TIMESTAMP");


        List<SpcPpkReportGridOut> resultDataList = mySql_QueryRunner_QueryListBySql(sqlVo, SpcPpkReportGridOut.class);




        /*** 计算Tool的稼动率排行 **/
        List<SpcPpkReportGridOut> gridOutBean_by_ppk_List = new ArrayList<>();
        resultDataList.stream().forEach(data -> {
            SpcPpkReportGridOut gridOutBean = new SpcPpkReportGridOut();
            gridOutBean.setTool_id_fk(data.getTool_id_fk());
            if ("T".equalsIgnoreCase(data.getTool_id_fk())) {
                gridOutBean.setTool_desc("-设备汇总数据-");
            }else {
                gridOutBean.setTool_desc(data.getTool_desc());
            }
            gridOutBean.setGrp_name_fk(data.getGrp_name_fk());
            gridOutBean.setGrp_no_fk(data.getGrp_no_fk());
            gridOutBean.setChart_name_fk(data.getChart_name_fk());
            gridOutBean.setChart_no_fk(data.getChart_no_fk());
            gridOutBean.setCol_typ_fk(data.getCol_typ_fk());
            gridOutBean.setData_group_fk(data.getData_group_fk());
            String ppk_string = data.getCalc_ppk() == null ? "0.00" : data.getCalc_ppk();
            gridOutBean.setCalc_ppk(formatDouble2(ppk_string).toString());
            gridOutBean.setRpt_typ_fk(data.getRpt_typ_fk());
            gridOutBean.setRpt_b_date(data.getRpt_b_date());
            gridOutBean.setRpt_e_date(data.getRpt_e_date());
            gridOutBean.setTime_range(data.getRpt_b_date() + " - " + data.getRpt_e_date());
            gridOutBean_by_ppk_List.add(gridOutBean);
        });
        LKingSortUtil.sort(gridOutBean_by_ppk_List, new String[] { "tool_desc", "time_range", "calc_ppk" }, new boolean[] { true, false, true });

        return gridOutBean_by_ppk_List;
    }


}
